package com.sj.parksdk.adapter.base;

import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.adapter.recyclerview.XRecyclerAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 通用的RecyclerView适配器
 *
 * @author XUE
 * @date 2017/9/10 18:30
 */
public abstract class BaseRecyclerAdapter<T> extends XRecyclerAdapter<T, RecyclerViewHolder> {
    // 保存选中状态
    protected boolean[] checkedFlags;

    // 是否为单选
    protected boolean radio;
    // 是否有选择框
    protected boolean check;
    // 选中项的游标
    protected int selectPosition;


    public BaseRecyclerAdapter() {
        super();
        checkedFlags = new boolean[mData.size()];
    }

    public BaseRecyclerAdapter(Collection<T> list) {
        super(list);
        checkedFlags = new boolean[mData.size()];
    }

    public BaseRecyclerAdapter(T[] data) {
        super(data);
        checkedFlags = new boolean[mData.size()];
    }

    public BaseRecyclerAdapter setRadio(boolean radio) {
        this.radio = radio;
        return this;
    }

    public BaseRecyclerAdapter setCheck(boolean check) {
        this.check = check;
        return this;
    }

    /**
     * 适配的布局
     *
     * @param viewType
     * @return
     */
    protected abstract int getItemLayoutId(int viewType);

    @NonNull
    @Override
    protected RecyclerViewHolder getViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(inflateView(parent, getItemLayoutId(viewType)));
    }

    /**
     * 更改选中状态并更新UI
     *
     * @param position
     * @param checked
     */
    public void setChecked(int position, boolean checked) {
        if (radio && checked) {
            clearAllCheckedFlags();
            setCheckedFlags(position, checked);
        } else {
            setCheckedFlags(position, checked);
        }
        baseNotify();
    }

    /**
     * 更改选中状态不更新UI
     *
     * @param position
     * @param checked
     */
    public void setCheckedFlags(int position, boolean checked) {
        if (checkedFlags != null && position < checkedFlags.length) {
            checkedFlags[position] = checked;
        }
        baseNotify();
    }

    /**
     * 清空选中不更新UI
     */
    private void clearAllCheckedFlags() {
        if (mData != null && mData.size() > 0) {
            checkedFlags = new boolean[mData.size()];
        } else {
            checkedFlags = null;
        }
    }

    /**
     * 获取选择的行
     *
     * @return
     */
    public List<T> getCheckedList() {
        List<T> arrayList = new ArrayList<T>();
        if (check && mData != null) {
            for (int index = 0; index < mData.size(); index++) {
                if (checkedFlags[index]) {
                    arrayList.add(mData.get(index));
                    selectPosition = index;
                }
            }
        }
        if (arrayList == null || arrayList.size() == 0) {
            return null;
        }
        return arrayList;
    }

    ;

    public T getChecked() {
        if (check && mData != null) {
            for (int index = 0; index < mData.size(); index++) {
                if (checkedFlags[index]) {
                    selectPosition = index;
                    return mData.get(index);

                }
            }
        }
        return null;
    }

    ;

    /**
     * 清空选中并更新UI
     */
    public void clearAllChecked() {
        if (mData != null && mData.size() > 0) {
            checkedFlags = new boolean[mData.size()];
        } else {
            checkedFlags = null;
        }
        this.notifyDataSetChanged();
    }

    /**
     * 全选
     */
    public void checkAll() {
        if (mData != null && mData.size() > 0) {
            checkedFlags = new boolean[mData.size()];
            for (int index = 0; index < checkedFlags.length; index++) {
                checkedFlags[index] = true;
            }
        } else {
            checkedFlags = null;
        }
        this.notifyDataSetChanged();
    }

    /**
     * 是否存在选中项
     */
    public boolean hasChecked() {
        if (check && mData != null) {
            for (int index = 0; index < mData.size(); index++) {
                if (checkedFlags[index]) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean whetherItemChecked(int position) {
        if (check && mData != null && mData.size() > position) {
            if (checkedFlags[position]) {
                return true;
            }
        }
        return false;
    }

    public void baseNotify() {
        this.notifyDataSetChanged();
        if (mData == null) return;
        if (checkedFlags == null) {
            checkedFlags = new boolean[mData.size()];
            return;
        }
        if (checkedFlags.length != mData.size()) {
            boolean[] newCheckedFlags = new boolean[mData.size()];
            for (int index = 0; index < newCheckedFlags.length; index++) {
                if (index < checkedFlags.length) {
                    newCheckedFlags[index] = checkedFlags[index];
                }
            }
            checkedFlags = newCheckedFlags;
        }
    }

    public int getItemPosition(T item) {
        return item != null && mData != null && !mData.isEmpty() ? mData.indexOf(item) : -1;
    }

}
